Theme
The ak-theme
component allows you to define theme variables for components that can affect how they look and feel.
Try it out
Interact with a live demo of the ak-theme component.
Installation
npm install ak-theme
Using the component
HTML
The ak-theme
package exports the Theme Skate component.
Import the component in your JS resource:
bundle.js
import 'ak-theme';
Now you can use the defined tag in your HTML markup:
index.html
<html>
<head>
<script src="bundle.js"></script>
</head>
<body>
<ak-theme></ak-theme>
</body>
or from within another JavaScript resource:
import Theme from 'ak-theme';
const component = new Theme();
document.body.appendChild(component);
React
This is a standard web component, if you want to use it in your React app, use the Skate.js React integration.
import Theme from 'ak-theme';
import reactify from 'skatejs-react-integration';
const ReactComponent = reactify(Theme, {});
ReactDOM.render(<ReactComponent />, container);
Theming components
Theming is a two-part process:
-
Declaring a theme.
-
Make a component themeable.
How to declare a theme
If you want to theme a component, declare a theme and specify some variables:
<ak-theme id="my-component">
<ak-theme-var name="background" value="blue" />
<ak-theme-var name="text" value="white" />
</ak-theme>
This example declares a theme for a component called my-component
and exposes two variables:
background
text
How to make a themeable component
To make a component able to be themed, use the themeable()
function that is exported from ak-theme
.
import { define } from 'skatejs';
import { themeable } from 'ak-theme';
export default define('my-component', themeable({
render(elem) {
return (
<style>
background-color: {elem.themeVars.background}
</style>
);
}
}));
The function themeable()
does several things for you:
themeVars
are always populated with the corresponding theme's variables.themeVars
is updated if the theme is updated.themeVars
is updated if the component's theme is changed.
When using themeable()
you only have to worry about rendering your component based on some theme variables.
How to set a component's theme
A themeable component's theme is a theme that has an id
that matches its tag name. However, if you need to change the theme a component should have, you can set its themeName
property. This property is also reflected, so you can use the theme-name
attribute just the same.
This is handy when the display of a particular component may changes depending on the parent's context.
<ak-button>My button</ak-button>
<ak-navigation>
<ak-button theme-name="nav-button">Navigation button</ak-button>
</ak-navigatoin>
Members
- themeName :
string
The id of the theme the component should use. This defaults to the tag name
of the component.
- themeProps :
string
The deserialised variables from the component's current theme. These are
automatically kept in sync.
themeName : string
The id of the theme the component should use. This defaults to the tag name
of the component.
Kind: global variable
themeProps : string
The deserialised variables from the component's current theme. These are
automatically kept in sync.
Kind: global variable
Support and feedback
We're here to help!
Let us know what you think of our components and docs, your feedback is really important for us.
Ask a question in our forum.
Check if someone has already asked the same question before.
Create a support ticket
Are you in trouble? Let us know!